package com.bjsxt.base.conn007_volatile;

public class RunThread extends Thread {
    // 如果不加 volatile 关键字，子线程中获取不到 isRunning 更改为了 false
    private volatile boolean isRunning = true;
    private void setRunning(boolean isRunning) {
        this.isRunning = isRunning;
    }

    public void run() {
        System.out.println("进入 run 方法..");
        while (isRunning == true) {
            // ..
        }
        System.out.println("线程停止");
    }

    public static void main(String[] args) throws InterruptedException {
        RunThread rt = new RunThread();
        rt.start();
        Thread.sleep(3000);
        rt.setRunning(false);
        System.out.println("isRunning 的值已经被设置了 false");
        Thread.sleep(1000);
        System.out.println(rt.isRunning);
    }
}
